﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Chanel : System.Web.UI.Page
{
    ChanelBusiness cb = new ChanelBusiness();
    private int id = Helper.getQuery("id");
    protected void Page_Load(object sender, EventArgs e)
    {
        bindTreeView(TreeView2.Nodes, 0);
        bindDropDown();
        if (id > 0)
        {
            btnDelete.Visible = true;
            ChanelEntities ce = cb.getChannel(id);
            txtTenHienThi.Text = ce._channelName;
            DropDownList1.SelectedValue = ce._channelParentID.ToString();
            if (ce._channelLocked == "true")
                CheckBox1.Checked = true;
            btnAddNew.Text = "Sửa";
            ViewState["channelPriority"] = int.Parse(ce._channelPriority.ToString());
            int max = cb.max(ce._channelParentID);
            int min = cb.min(ce._channelParentID);
            if (max == cb.max(ce._channelParentID))
                this.ibUp.Visible = false;
            if (min == cb.min(ce._channelParentID))
                this.ibDown.Visible = false;
        }
        else
        {
            btnDelete.Visible = false;
            btnAddNew.Text = "Thêm mới";
        }

    }

    public void bindTreeView(TreeNodeCollection node,int chanelParent)
    {
        DataTable dt = cb.getAllChannel();
        int j = 0;
        if (dt != null)
        {
            ChanelEntities ce = null;
            while (j < dt.Rows.Count)
            {
                DataRow dr = dt.Rows[j];
                ce = new ChanelEntities();
                ce._channelID = int.Parse(dr["channelID"].ToString());
                ce._channelName = dr["channelName"].ToString();
                ce._channelLink = "Chanel.aspx?id="+ce._channelID;
                ce._channelParentID = int.Parse(dr["channelParentID"].ToString());
                ce._channelPriority = int.Parse(dr["channelPriority"].ToString());
                ce._channelLocked = dr["channelLocked"].ToString();
                ce._channelLanguage = int.Parse(dr["channelLanguage"].ToString());
                ce._channelRequireApprove = dr["channelRequireApprove"].ToString();
                if (ce._channelID == chanelParent)
                {
                    dt.Rows.Remove(dr);
                    TreeNode tn = new TreeNode(ce._channelName, ce._channelID.ToString(), null, ce._channelLink, "");
                    tn.SelectAction = TreeNodeSelectAction.Expand;
                    node.Add(tn);
                    this.bindTreeView(tn.ChildNodes, ce._channelID);
                    continue;
                }
                j++;
            }
        }
    }
    public void bindDropDown()
    {
        DropDownList1.DataSource = cb.getAllChannel();
        DropDownList1.DataTextField = "channelName";
        DropDownList1.DataValueField = "channelID";
        DropDownList1.DataBind();

    }

    protected void ibUp_Click(object sender, ImageClickEventArgs e)
    {
        if (id > 0)
        {
            if (DataAccess.getExecProcedure("sp_UpChannel", new string[] { "@channelID" }, new string[] { id.ToString() }) > 0)
                Response.Redirect("Chanel.aspx");
            else
                Label1.Text = "Không thay đổi được";
        }
    }
    protected void ibDown_Click(object sender, ImageClickEventArgs e)
    {
        if (id > 0)
        {
            if (DataAccess.getExecProcedure("sp_DownChannel", new string[] { "@channelID" }, new string[] { id.ToString() }) > 0)
                Response.Redirect("Chanel.aspx");
            else
                Label1.Text = "Không thay đổi được";
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ChanelEntities ce = new ChanelEntities();
        ce._channelID = id;
        ce._channelName = txtTenHienThi.Text;
        ce._channelLink = "Default.aspx";
        ce._channelParentID = int.Parse(DropDownList1.SelectedValue);
        ce._channelLocked = CheckBox1.Checked ? "1" : "0";
        if (btnAddNew.Text == "Sửa")
        {
            if(cb.Edit(ce))
                Response.Redirect("Chanel.aspx");
            else
                Label1.Text="Không sửa được";
        }
        if (btnAddNew.Text == "Thêm mơi")
        {
            if(cb.Insert(ce))
                Response.Redirect("Chanel.aspx");
            else
                Label1.Text = "Không thêm được";
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (id > 0)
        {
            if(cb.Delete(id))
                Response.Redirect("Chanel.aspx");
            else
                Label1.Text = "Không xóa được";
        }
    }
    protected void lbAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("Chanel.aspx?id=0");
    }
}